package com.finalproject.entity;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Scanner;

//import com.finalproject.entity.CafeDatabase.CafeCollection;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;

public class SerializerDesrializer{
	SerializerDesrializer f=new SerializerDesrializer();
	public static void main(String[] args) throws IOException{
		//SNACK ITEM FIELDS
		/*private String name;
		private Double price;
		private String description;
		private String image;
		private Integer calories;
		private Double fat;
		private Double cholestrol;
		private Double carbs;
		private Double sodium;
		private Double protein;*/

		//you can find the nutrition values of the snack items in: http://www.livestrong.com/myplate
		//when you type in the snack name and add it to and click on "I ate this" it displays all the fields in the bottom
		//i wanted to make the values more realistic ;)
		//we need at least 9 different snack items
		//we'll also need cafeitems. you can use http://www.quiznos.com/Libraries/PDFs/NutritionalInfo.sflb.ashx
		//again, a few cafeItems will be enough. Make sure you also set the prep time
		GregorianCalendar cl;

		// 1 M&Ms		
		SnackItem sa1 = new SnackItem();
		sa1.setName("Twix");
		sa1.setPrice(1.00);
		sa1.setCalories(210);
		sa1.setFat(9.0);
		sa1.setCholestrol(5.0);
		sa1.setSodium(30.0);
		sa1.setCarbs(30.0);		
		sa1.setProtein(2.0);

		// 2 Nature Valley Granola Bar
		SnackItem sa2 = new SnackItem();
		sa2.setName("Nature Valley Granola Bar");
		sa2.setPrice(1.50);
		sa2.setCalories(160);
		sa2.setFat(7.0);
		sa2.setCholestrol(0.0);
		sa2.setSodium(150.0);
		sa2.setCarbs(22.0);
		sa2.setProtein(4.0);

		// 3 Snickers
		SnackItem sa3 = new SnackItem();
		sa3.setName("Snickers");
		sa3.setPrice(1.00);
		sa3.setCalories(280);
		sa3.setFat(14.0);
		sa3.setCholestrol(5.0);
		sa3.setSodium(140.0);
		sa3.setCarbs(35.0);		
		sa3.setProtein(4.0);

		// 4 Lays
		SnackItem sa4 = new SnackItem();
		sa4.setName("Lays");
		sa4.setPrice(1.50);
		sa4.setCalories(75);
		sa4.setFat(0.0);
		sa4.setCholestrol(0.0);
		sa4.setSodium(200.0);
		sa4.setCarbs(17.0);
		sa4.setProtein(2.0);

		// 5 Cheez-It
		SnackItem sa5 = new SnackItem();
		sa5.setName("Cheez-It");
		sa5.setPrice(1.50);
		sa5.setCalories(150);
		sa5.setFat(8.0);
		sa5.setCholestrol(0.0);
		sa5.setSodium(250.0);
		sa5.setCarbs(17.0);		
		sa5.setProtein(3.0);

		// 6 Nutter Butters
		SnackItem sa6 = new SnackItem();
		sa6.setName("Nutter Butters");
		sa6.setPrice(1.50);
		sa6.setCalories(260);
		sa6.setFat(11.0);
		sa6.setCholestrol(0.0);
		sa6.setSodium(190.0);
		sa6.setCarbs(37.0);
		sa6.setProtein(4.0);

		// 7 Doritos
		SnackItem sa7 = new SnackItem();
		sa7.setName("Doritos");
		sa7.setPrice(1.25);
		sa7.setCalories(250);
		sa7.setFat(13.0);
		sa7.setCholestrol(0.0);
		sa7.setSodium(310.0);
		sa7.setCarbs(30.0);		
		sa7.setProtein(4.0);

		// 8 Animal Crackers
		SnackItem sa8 = new SnackItem();
		sa8.setName("Animal Crackers");
		sa8.setPrice(1.50);
		sa8.setCalories(120);
		sa8.setFat(2.0);
		sa8.setCholestrol(0.0);
		sa8.setSodium(105.0);
		sa8.setCarbs(24.0);
		sa8.setProtein(2.0);

		// 9 Skittles
		SnackItem sa9 = new SnackItem();
		sa9.setName("Skittles");
		sa9.setPrice(1.25);
		sa9.setCalories(250);
		sa9.setFat(2.5);
		sa9.setCholestrol(0.0);
		sa9.setSodium(10.0);
		sa9.setCarbs(56.0);		
		sa9.setProtein(0.0);

		// 1 Cheese Pizza		
		CafeItem ca1 = new CafeItem();
		ca1.setName("Cheese Pizza");
		ca1.setPrice(5.00);
		ca1.setPrepTime(15);
		ca1.setCalories(590);
		ca1.setFat(24.0);
		ca1.setCholestrol(55.0);
		ca1.setSodium(1290.0);
		ca1.setCarbs(69.0);		
		ca1.setProtein(26.0);

		// 2 Taco Salad
		CafeItem ca2 = new CafeItem();
		ca2.setName("Taco Salad");
		ca2.setPrice(6.50);
		ca2.setPrepTime(5);
		ca2.setCalories(770);
		ca2.setFat(41.0);
		ca2.setCholestrol(60.0);
		ca2.setSodium(1650.0);
		ca2.setCarbs(75.0);
		ca2.setProtein(27.0);

		// 3 Chicken Burrito Supreme
		CafeItem ca3 = new CafeItem();
		ca3.setName("Chicken Burrito Supreme");
		ca3.setPrice(3.00);
		ca3.setPrepTime(10);
		ca3.setCalories(390);
		ca3.setFat(12.0);
		ca3.setCholestrol(40.0);
		ca3.setSodium(1420.0);
		ca3.setCarbs(51.0);		
		ca3.setProtein(21.0);

		// 4 Turkey Ranch and Swiss Sandwich
		CafeItem ca4 = new CafeItem();
		ca4.setName("Turkey Ranch & Swiss");
		ca4.setPrice(6.50);
		ca4.setPrepTime(10);
		ca4.setCalories(280);
		ca4.setFat(55.0);
		ca4.setCholestrol(20.0);
		ca4.setSodium(980.0);
		ca4.setCarbs(42.0);
		ca4.setProtein(16.0);

		CafeItem ca5 = new CafeItem();
		ca5.setName("Chicken Soup");
		ca5.setPrice(4.50);
		ca5.setPrepTime(10);
		ca5.setCalories(280);
		ca5.setFat(55.0);
		ca5.setCholestrol(20.0);
		ca5.setSodium(980.0);
		ca5.setCarbs(42.0);
		ca5.setProtein(16.0);

		CafeItem ca6 = new CafeItem();
		ca6.setName("Cheeseburger");
		ca6.setPrice(6.00);
		ca6.setPrepTime(10);
		ca6.setCalories(390);
		ca6.setFat(12.0);
		ca6.setCholestrol(40.0);
		ca6.setSodium(1420.0);
		ca6.setCarbs(51.0);		
		ca6.setProtein(21.0);

		CafeItem ca7 = new CafeItem();
		ca7.setName("Quesadilla");
		ca7.setPrice(5.00);
		ca7.setPrepTime(12);
		ca7.setCalories(690);
		ca7.setFat(24.0);
		ca7.setCholestrol(55.0);
		ca7.setSodium(1290.0);
		ca7.setCarbs(69.0);		
		ca7.setProtein(26.0);

		CafeItem ca8 = new CafeItem();
		ca8.setName("Chicken Pasta Alfredo");
		ca8.setPrice(7.50);
		ca8.setPrepTime(8);
		ca8.setCalories(770);
		ca8.setFat(41.0);
		ca8.setCholestrol(60.0);
		ca8.setSodium(1650.0);
		ca8.setCarbs(75.0);
		ca8.setProtein(27.0);

		CafeItem ca9 = new CafeItem();
		ca9.setName("Veggie Burger");
		ca9.setPrice(6.00);
		ca9.setPrepTime(8);
		ca9.setCalories(570);
		ca9.setFat(41.0);
		ca9.setCholestrol(60.0);
		ca9.setSodium(1650.0);
		ca9.setCarbs(75.0);
		ca9.setProtein(27.0);

		CardHolder doeFamily=new CardHolder(123456,1234);

		doeFamily.addMember("John Doe", 01);	
		//john is a multiuser cardholder

		doeFamily.addMember("Jane Doe", 02);	
		//jane is a multiuser cardholder

		CardHolder mojo=new CardHolder(234567, 1234);
		mojo.addMember("Mojo Risin", 00);		
		//Mr. Mojo Risin is a regular cardholder

		CardHolder ziggy=new CardHolder(345678, 1234);
		ziggy.addMember("Ziggy Stardust", 00);


		//block 1 ================ Doe Family================
		doeFamily.selectCurrentMember(01);	
		//John Doe so the following changes will effect his profile
		doeFamily.addFunds(130.0);
		try {
			//CAREFUL HERE! I'm setting the date Feb 22 with new 
			//GregorianCalendar(2012, 1,22), NOT new 
			//GregorianCalendar(2012, 2,22)
			//Jan is 0, Feb is 1, March is 2 etc.
			cl=new GregorianCalendar(2012, 2,6);
			doeFamily.buyItem(sa2, cl);
			doeFamily.buyItem(sa1, cl);
			doeFamily.buyItem(ca3, cl);
			doeFamily.buyItem(sa6, cl);

			doeFamily.logout(01);	
			doeFamily.selectCurrentMember(02);

			cl=new GregorianCalendar(2012, 2,7);
			doeFamily.buyItem(ca2, cl);
			doeFamily.buyItem(ca4, cl);
			doeFamily.buyItem(sa2, cl);
			doeFamily.buyItem(sa5, cl);

			doeFamily.logout(02);	
			doeFamily.selectCurrentMember(01);

			cl=new GregorianCalendar(2012, 2, 7);
			doeFamily.buyItem(ca2, cl);
			doeFamily.buyItem(sa8, cl);


			cl=new GregorianCalendar(2012, 2, 8);
			doeFamily.buyItem(sa4, cl);
			doeFamily.buyItem(sa9, cl);
			doeFamily.buyItem(sa2, cl);
			doeFamily.buyItem(ca2, cl);
			doeFamily.buyItem(ca4, cl);
			doeFamily.buyItem(sa5, cl);

			doeFamily.logout(01);	
			doeFamily.selectCurrentMember(02);

			doeFamily.buyItem(sa9, cl);
			doeFamily.buyItem(sa2, cl);
			doeFamily.buyItem(sa7, cl);

			doeFamily.logout(02);	
			doeFamily.selectCurrentMember(01);

			cl=new GregorianCalendar(2012, 2,9);
			doeFamily.buyItem(sa4, cl);
			doeFamily.buyItem(sa9, cl);
			doeFamily.buyItem(ca2, cl);
			doeFamily.buyItem(ca4, cl);

			cl=new GregorianCalendar(2012, 2 ,10);
			doeFamily.buyItem(sa2, cl);
			doeFamily.buyItem(sa1, cl);

			doeFamily.logout(01);	
			doeFamily.selectCurrentMember(02);


			cl=new GregorianCalendar(2012, 2, 11);
			doeFamily.buyItem(sa4, cl);
			doeFamily.buyItem(sa1, cl);
			doeFamily.buyItem(sa5, cl);

			doeFamily.logout(02);	
			doeFamily.selectCurrentMember(01);

			cl=new GregorianCalendar(2012, 2,12);
			doeFamily.buyItem(sa6, cl);
			doeFamily.buyItem(sa4, cl);
			doeFamily.buyItem(ca3, cl);
			doeFamily.buyItem(ca4, cl);

			doeFamily.logout(01);	
			doeFamily.selectCurrentMember(02);

			doeFamily.buyItem(sa2, cl);
			doeFamily.buyItem(ca4, cl);
			doeFamily.buyItem(sa1, cl);


			cl=new GregorianCalendar(2012, 2,13);
			doeFamily.buyItem(sa4, cl);
			doeFamily.buyItem(sa9, cl);
			doeFamily.buyItem(sa2, cl);
			doeFamily.buyItem(ca4, cl);
			doeFamily.buyItem(sa5, cl);

			doeFamily.logout(02);	
			doeFamily.selectCurrentMember(01);

			cl=new GregorianCalendar(2012, 2,14);
			doeFamily.buyItem(sa6, cl);
			doeFamily.buyItem(sa4, cl);
			doeFamily.buyItem(ca3, cl);
			doeFamily.buyItem(ca4, cl);

			doeFamily.logout(01);	
			//IMPORTANT! This is to save the information

		} catch (InsufficientFundsException e) {
			e.printStackTrace();
		}



		// block 3 ============== Single Member: Ziggy Stardust ===============
		ziggy.selectCurrentMember(00);	
		ziggy.addFunds(600.0);
		try {
			cl=new GregorianCalendar(2012, 2,9);
			ziggy.buyItem(sa2, cl);
			ziggy.buyItem(sa1, cl);

			cl=new GregorianCalendar(2012, 2,10);
			ziggy.buyItem(sa2, cl);
			ziggy.buyItem(sa1, cl);


			cl=new GregorianCalendar(2012, 2, 11);
			ziggy.buyItem(sa4, cl);
			ziggy.buyItem(sa1, cl);
			ziggy.buyItem(sa5, cl);
			ziggy.buyItem(sa2, cl);
			ziggy.buyItem(sa7, cl);


			cl=new GregorianCalendar(2012, 2,12);
			ziggy.buyItem(sa2, cl);
			ziggy.buyItem(ca4, cl);
			ziggy.buyItem(sa1, cl);

			cl=new GregorianCalendar(2012, 2,13);
			ziggy.buyItem(sa4, cl);
			ziggy.buyItem(sa9, cl);
			ziggy.buyItem(sa2, cl);
			ziggy.buyItem(ca4, cl);
			ziggy.buyItem(sa5, cl);

			cl=new GregorianCalendar(2012, 2,14);
			ziggy.buyItem(sa6, cl);
			ziggy.buyItem(sa4, cl);
			ziggy.buyItem(ca3, cl);
			ziggy.buyItem(ca4, cl);


			ziggy.logout(00);	
			//IMPORTANT! This is to save the information

		} catch (InsufficientFundsException e) {
			e.printStackTrace();
		}


		// block 4 ================ Single Member: Mojo Ridin =================
		mojo.selectCurrentMember(00);	
		mojo.addFunds(500.0);
		try {
			cl=new GregorianCalendar(2012, 2,9);
			mojo.buyItem(sa2, cl);
			mojo.buyItem(sa1, cl);
			mojo.buyItem(sa3, cl);
			mojo.buyItem(sa4, cl);


			cl=new GregorianCalendar(2012, 2,10);
			mojo.buyItem(sa3, cl);
			mojo.buyItem(sa5, cl);


			cl=new GregorianCalendar(2012, 2, 11);
			mojo.buyItem(sa4, cl);
			mojo.buyItem(sa1, cl);
			mojo.buyItem(sa5, cl);
			mojo.buyItem(sa2, cl);
			mojo.buyItem(sa7, cl);


			cl=new GregorianCalendar(2012, 2,12);
			mojo.buyItem(sa2, cl);
			mojo.buyItem(ca4, cl);
			mojo.buyItem(sa1, cl);

			cl=new GregorianCalendar(2012, 2,13);
			mojo.buyItem(sa4, cl);
			mojo.buyItem(sa9, cl);
			mojo.buyItem(sa2, cl);
			mojo.buyItem(ca4, cl);
			mojo.buyItem(sa5, cl);

			cl=new GregorianCalendar(2012, 2,14);
			mojo.buyItem(sa6, cl);
			mojo.buyItem(sa4, cl);
			mojo.buyItem(ca3, cl);
			mojo.buyItem(ca4, cl);


			mojo.logout(00);	
			//IMPORTANT! This is to save the information

		} catch (InsufficientFundsException e) {
			e.printStackTrace();
		}



		List<Cafe> cafes=new ArrayList<Cafe>();
		Cafe cafe1=new Cafe(101, new int[] {220,125});
		cafe1.setName("Cafe Pi");
		cafe1.add(ca1);
		cafe1.add(ca2);
		cafe1.add(ca3);
		cafe1.add(ca4);
		cafes.add(cafe1);

		Cafe cafe2=new Cafe(102, new int[] {20,250});
		cafe2.setName("Tesla Cafe");
		cafe2.add(ca5);
		cafe2.add(ca6);
		cafe2.add(ca7);
		cafe2.add(ca8);
		cafes.add(cafe2);

		Cafe cafe3=new Cafe(103, new int[] {250,230});
		cafe3.setName("Sunshine Cafe");
		cafe3.add(ca9);
		cafe3.add(ca1);
		cafe3.add(ca3);
		cafe3.add(ca5);
		cafes.add(cafe3);

		Cafe cafe4=new Cafe(104,new int[] {430,260});
		cafe4.setName("Eastwest Cafe");
		cafe4.add(ca2);
		cafe4.add(ca4);
		cafe4.add(ca6);
		cafe4.add(ca8);
		cafes.add(cafe4);

		List<VendingMachine> vms=new ArrayList<VendingMachine>();
		VendingMachine vm1=new VendingMachine(201,new int[] {95,125});
		vm1.add(sa1,10);
		vm1.add(sa2,10);
		vm1.add(sa3,10);
		vm1.add(sa4,10);
		vm1.add(sa5,10);
		vm1.add(sa6,10);
		vm1.add(sa7,10);
		vm1.add(sa8,10);
		vm1.add(sa9,10);

		VendingMachine vm2=new VendingMachine(202,new int[] {20,300});
		vm2.add(sa1,10);
		vm2.add(sa2,10);
		vm2.add(sa3,10);
		vm2.add(sa4,10);
		vm2.add(sa5,10);
		vm2.add(sa6,10);
		vm2.add(sa7,10);
		vm2.add(sa8,10);
		vm2.add(sa9,10);

		VendingMachine vm3=new VendingMachine(203,new int[] {250,250});
		vm3.add(sa1,10);
		vm3.add(sa2,10);
		vm3.add(sa3,10);
		vm3.add(sa4,10);
		vm3.add(sa5,10);
		vm3.add(sa6,10);
		vm3.add(sa7,10);
		vm3.add(sa8,10);
		vm3.add(sa9,10);

		VendingMachine vm4=new VendingMachine(204,new int[] {300,400});
		vm4.add(sa1,10);
		vm4.add(sa2,10);
		vm4.add(sa3,10);
		vm4.add(sa4,10);
		vm4.add(sa5,10);
		vm4.add(sa6,10);
		vm4.add(sa7,10);
		vm4.add(sa8,10);
		vm4.add(sa9,10);

		vms.add(vm1);
		vms.add(vm2);
		vms.add(vm3);
		vms.add(vm4);

		//Gson gson=new GsonBuilder().setPrettyPrinting().create();
		Gson gson=new Gson();
		String doe=gson.toJson(doeFamily);
		try {
			//the file name will be ID_123456.txt
			BufferedWriter out = new BufferedWriter(new FileWriter("ID_"+doeFamily.getId()+".txt"));
			out.write(doe);
			out.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}



		String mr=gson.toJson(mojo);
		try {
			//the file name will be ID_234567.txt
			BufferedWriter out = new BufferedWriter(new FileWriter("ID_"+mojo.getId()+".txt"));
			out.write(mr);
			out.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}




		String zs=gson.toJson(ziggy);
		try {
			//the file name will be ID_345678.txt
			BufferedWriter out = new BufferedWriter(new FileWriter("ID_"+ziggy.getId()+".txt"));
			out.write(zs);
			out.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}	

		
		String cafeData=gson.toJson(cafes);
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter("cafe_database.txt"));
			out.write(cafeData);
			out.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}

		String vendingData=gson.toJson(vms);
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter("vending_database.txt"));
			out.write(vendingData);
			out.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}

		
	}
	
}


